﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using nelech.Models;
using nelech.Models.DBModel;
using nelech.Controllers.Utils;

namespace nelech.Controllers
{
    public partial class TrackController : Controller
    {
        nelechDBEntities db = new nelechDBEntities();
        //
        // GET: /Track/

        public ActionResult Index()
        {
            var tracks = db.Track.Where(p => p.publish == 1).ToList();
            tracks = tracks.OrderBy(p => p.number, new ReverseIntComparer()).ToList();
          
           
            if (tracks.Count() > 5)
                tracks = tracks.GetRange(0, 5).ToList();
            var recommended = db.RecommendedTrack.ToList();
            var recommendedTrack = new List<Track>();
            foreach (var recTrack in recommended)
            {
                var track = db.Track.First(p => p.number == recTrack.id);
                if (track.publish == 1)
                {
                    recommendedTrack.Add(track);
                    LoadTrackRelations(track);
                    track.Image.OrderBy(p => p.path);
                }
            }
            foreach (var track in tracks)
            {
                LoadTrackRelations(track);
                track.Image.OrderBy(p => p.path);
            }
            var trackArry = new Object[2];
            trackArry[0] = tracks;
            trackArry[1] = recommendedTrack;
            return View(trackArry);



        }

        public ActionResult ListTrackByRegion(int id)
        {
            var tracks = db.Track.Where(p => p.publish == 1 && p.regeon == id).ToList();
            tracks = tracks.OrderBy(p => p.number, new ReverseIntComparer()).ToList();
            if (id == 1)
            {
                ViewData["Title"] = "גולן";
            }
            else if(id ==13){
                ViewData["Title"] = "נגב";
                tracks = db.Track.Where(p => p.publish == 1 && p.regeon >= 13 && p.regeon<=19 ).ToList();
            }
            else{
            var region = db.Regeon.First(p => p.id == id);
            ViewData["Title"] = region.text;
            }
            foreach (var track in tracks)
            {
                LoadTrackRelations(track);
                track.Image.OrderBy(p => p.path);
            }
            return View(tracks);

        }
        public ActionResult TrackInfo(int id)
        {
            var track = db.Track.First(p => p.number == id);
            LoadTrackRelations(track);
            var lon = track.lon;
            var lat = track.lat;
            var adverts = db.Advert.Where(p => p.publish.HasValue && p.publish.Value == 1);
            int advertCount = Utils.AdvertControllerUtils.GetAdvertCount(6,track.regeon, db);
            var nearAttrs = db.Attraction.AsEnumerable().Where(p => p.publish == true && (SharedUtils.Distance(lon, lat, p.lon, p.lat) <= 0.2)).AsEnumerable().OrderBy(p => p, new NearAttrComparer(lon, lat)).Take(4);
          
            var advertsArray = Utils.AdvertControllerUtils.GetUniqueArray(advertCount);
            ViewData["advertsArray"] = advertsArray;
            ViewData["advertCount"] = advertsArray == null ? 0 : advertsArray.Length;
            var nearPlaces = db.Place.AsEnumerable().Where(p => p.publish == 1 && SharedUtils.Distance(lon, lat, p.lon, p.lat) <= 0.2).AsEnumerable().OrderBy(p => p, new NearPlaceComparer(lon, lat)).Take(4);
            var nearSprings = db.Maayan.AsEnumerable().Where(p => p.publish == 1 && SharedUtils.Distance(lon, lat, p.lon, p.lat) <= 0.2).AsEnumerable().OrderBy(p => p, new NearMaayanComparer(lon, lat)).Take(4);
            var nearTracks = db.Track.AsEnumerable().Where(p => p.publish == 1 && (SharedUtils.Distance(lon, lat, p.lon, p.lat) <= 0.2) && p.number != id).AsEnumerable().OrderBy(p => p, new NearTrackComparer(lon, lat)).Take(4);
            var nearZimers = db.Zimer.AsEnumerable().Where(p => p.publish == 1 && (SharedUtils.Distance(lon, lat, p.lon, p.lat) <= 0.2)).AsEnumerable().OrderBy(p => p, new NearZimerComparer(lon, lat)).Take(4);

            var nearLinks = new NearLinks()
            {
                nearPlaces = nearPlaces.ToList(),
                nearSprings = nearSprings.ToList(),
                nearTracks = nearTracks.ToList(),
                nearZimers = nearZimers.ToList(),
                nearAttractions = nearAttrs.ToList()
            };
            ViewData["FeedBack"] = new Feedbacks() { type = 1, parentId = id };
            ViewData["ListFeedbacks"] = db.Feedback.Where(p => p.parent_id == id && p.FeedbackType.id == 1);

            ViewData["NearLinks"] = nearLinks;
            return View(track);
        }


        public ActionResult ListFreeTrack(int id)
        {
            var tracks = db.Track.Where(p => p.publish == 1 && p.Cost1.id == id).ToList();
            var listTrack = new ListTrack() { tracks = tracks, isSearch = false, isFree = id, isEmpty = false };
            if (tracks.Count() < 1)
            {
                listTrack.isEmpty = true;
                tracks = db.Track.ToList();
            }
            foreach (var track in tracks)
            {
                LoadTrackRelations(track);
                track.Image.OrderBy(p => p.path);
            }
            tracks = tracks.OrderBy(p => p.number, new ReverseIntComparer()).ToList();
            listTrack.tracks = tracks;
            return View("Search", listTrack);
        }


        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Search(int regeon, int season, int difficulty, int water, int duration, int traffic, string searchString)
        {
            var tracks = db.Track.Where(p => p.publish == 1).ToList();

            if (regeon != 21)
                tracks = tracks.Where(p => { p.Regeon1Reference.Load(); return p.Regeon1.id == regeon; }).ToList();
            if (season != 1)
            {
                var seasonMatch = new List<Track>();
                foreach (var track in tracks)
                {
                    track.Season.Load();
                    if (track.Season.Select(s => s.id).Contains(season))
                    {
                        seasonMatch.Add(track);
                    }
                }
                tracks = seasonMatch;
            }
            if (difficulty != 1)
            {

                foreach (var track in tracks)
                {
                    track.Difficulty1Reference.Load();
                }
                tracks = tracks.Where(p => p.Difficulty1.id == difficulty).ToList();
            }
            if (water != 0)
                tracks = tracks.Where(p => p.water == water).ToList();
            if (duration != 1)
            {
                foreach (var track in tracks)
                {
                    track.Duration1Reference.Load();
                }
                tracks = tracks.Where(p => p.Duration1.id == duration).ToList();
            }
            if (traffic != 5)
            {
                var trafficMatch = new List<Track>();
                foreach (var track in tracks)
                {
                    track.Traffic.Load();
                    if (track.Traffic.Select(s => s.id).Contains(traffic))
                    {
                        trafficMatch.Add(track);
                    }
                }
                tracks = trafficMatch;
            }
            tracks = SharedUtils.FreeSearchTrack(tracks, searchString);

            var listTrack = new ListTrack() { isFree = 3, isSearch = true, tracks = tracks, isEmpty = false };
            if (tracks.Count() > 0)
            {
                
                tracks = tracks.OrderBy(p => p.number, new ReverseIntComparer()).ToList();
                if (tracks.Count() > 8)
                    tracks = tracks.GetRange(0, 8);
                foreach (var track in tracks)
                {
                    LoadTrackRelations(track);
                    track.Image.OrderBy(p => p.path);
                }
            }
            else
            {
                listTrack.isEmpty = true;
            }

            listTrack.tracks = tracks;

            var adverts = db.Advert.Where(p => p.publish.HasValue && p.publish.Value == 1);
            int advertCount = Utils.AdvertControllerUtils.GetAdvertCount(6, null, db);
            var advertsArray = Utils.AdvertControllerUtils.GetUniqueArray(advertCount);
            ViewData["advertsArray"] = advertsArray;
            ViewData["advertCount"] = advertsArray == null ? 0 : advertsArray.Length;
            return View(listTrack);

        }
        public ActionResult ListZimer()
        {
            var zimers = db.Zimer.Where(p => p.publish == 1).ToList();
            zimers = zimers.OrderBy(p => p.id, new ReverseIntComparer()).ToList();
            if (zimers.Count() > 5)
            {
                zimers = zimers.GetRange(0, 5).ToList();
            }
            var listZimer = new ListZimer() { zimers = zimers, isEmpty = false };
            foreach (var zimer in listZimer.zimers)
            {
                LoadZimerRelations(zimer);
                zimer.Image.OrderBy(p => p.path);
            }
            return View(listZimer);
        }
        public ActionResult ListZimerBySize(int id)
        {
            var zimers = db.Zimer.Where(p => p.publish == 1 ).ToList();
            var sizesZimme = new List<Zimer>();
            var size = db.Size.First(p => p.id == id);
            ViewData["Title"] = size.text;
            if (id == 2)
            {
                foreach (var zimmer in zimers)
                {
                    zimmer.Size.Load();
                    if (zimmer.Size.Select(s => s.id).Contains(3) || zimmer.Size.Select(s => s.id).Contains(2))
                    {
                        sizesZimme.Add(zimmer);
                    }
                }

                ViewData["Title"] = "צימרים משפחתיים";
            }
            else
            {
                foreach (var zimmer in zimers)
                {
                    zimmer.Size.Load();
                    if (zimmer.Size.Select(s => s.id).Contains(id))
                    {
                        sizesZimme.Add(zimmer);
                    }
                }
            }
            if (id == 1)
            {
                ViewData["Title"] = "צימרים זוגיים";
            }
            zimers = sizesZimme;
         
            zimers = zimers.OrderBy(p => p.id, new ReverseIntComparer()).ToList();
         
           
            foreach (var zimer in zimers)
            {
                LoadZimerRelations(zimer);
                zimer.Image.OrderBy(p => p.path);
            }
            return View(zimers);
        }
        public ActionResult ZimerInfo(int id)
        {
            var zimer = db.Zimer.First(p => p.id == id);
            zimer.Regeon1Reference.Load();
            zimer.Size.Load();
            zimer.Image.Load();
            var adverts = db.Advert.Where(p => p.publish.HasValue && p.publish.Value == 1);
            int advertCount = Utils.AdvertControllerUtils.GetAdvertCount(6, zimer.regeon, db);
            var advertsArray = Utils.AdvertControllerUtils.GetUniqueArray(advertCount);
          
            ViewData["advertsArray"] = advertsArray;
            ViewData["advertCount"] = advertsArray == null ? 0 : advertsArray.Length;
            var lon = zimer.lon;
            var lat = zimer.lat;
            var nearPlaces = db.Place.AsEnumerable().Where(p => p.publish == 1 && SharedUtils.Distance(lon, lat, p.lon, p.lat) <= 0.2).AsEnumerable().OrderBy(p => p, new NearPlaceComparer(lon, lat)).Take(4);
            var nearSprings = db.Maayan.AsEnumerable().Where(p => p.publish == 1 && SharedUtils.Distance(lon, lat, p.lon, p.lat) <= 0.2).AsEnumerable().OrderBy(p => p, new NearMaayanComparer(lon, lat)).Take(4);
            var nearTracks = db.Track.AsEnumerable().Where(p => p.publish == 1 && (SharedUtils.Distance(lon, lat, p.lon, p.lat) <= 0.2)).AsEnumerable().OrderBy(p => p, new NearTrackComparer(lon, lat)).Take(4);
            var nearZimers = db.Zimer.AsEnumerable().Where(p => p.publish == 1 && (SharedUtils.Distance(lon, lat, p.lon, p.lat) <= 0.2) && p.id != id).AsEnumerable().OrderBy(p => p, new NearZimerComparer(lon, lat)).Take(4);
  var nearAttrs = db.Attraction.AsEnumerable().Where(p => p.publish == true && (SharedUtils.Distance(lon, lat, p.lon, p.lat) <= 0.2)).AsEnumerable().OrderBy(p => p, new NearAttrComparer(lon, lat)).Take(4);
          
            var nearLinks = new NearLinks()
            {
                nearPlaces = nearPlaces.ToList(),
                nearSprings = nearSprings.ToList(),
                nearTracks = nearTracks.ToList(),
                nearZimers = nearZimers.ToList(),
                nearAttractions = nearAttrs.ToList()
            };
            ViewData["NearLinks"] = nearLinks;
            ViewData["FeedBack"] = new Feedbacks() { type = 4, parentId = id };
            ViewData["ListFeedbacks"] = db.Feedback.Where(p => p.parent_id == id && p.FeedbackType.id == 4);

            return View(zimer);
        }
        public ActionResult SearchZimer(int regeon, int size, string searchString)
        {
            var adverts = db.Advert.Where(p => p.publish.HasValue && p.publish.Value == 1);
            int advertCount = Utils.AdvertControllerUtils.GetAdvertCount(6, null, db);
            var advertsArray = Utils.AdvertControllerUtils.GetUniqueArray(advertCount);
            ViewData["advertsArray"] = advertsArray;
            ViewData["advertCount"] = advertsArray == null ? 0 : advertsArray.Length;
            var zimers = db.Zimer.Where(p => p.publish == 1).ToList();
            if (regeon != 21)
            {
                foreach (var zimer in zimers)
                {
                    zimer.Regeon1Reference.Load();
                }
                zimers = zimers.Where(p => p.Regeon1.id == regeon).ToList();
            }
            var sizeMatch = new List<Zimer>();
            foreach (var zimer in zimers)
            {
                zimer.Size.Load();
                if (zimer.Size.Select(s => s.id).Contains(size))
                {
                    sizeMatch.Add(zimer);
                }
            }
            zimers = sizeMatch;

            zimers = SharedUtils.FreeSearchZimer(zimers, searchString);
            var listZimer = new ListZimer() { isEmpty = false };
            if (zimers.Count() > 0)
            {

                foreach (var zimer in zimers)
                {
                    zimer.Regeon1Reference.Load();
                    zimer.Size.Load();
                    zimer.Image.Load();
                    zimer.Image.OrderBy(p => p.path);
                }
            }
            else
            {
                listZimer.isEmpty = true;
            }
            listZimer.zimers = zimers;
            return View("ListZimer", listZimer);
        }
    }
}
